using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartTracking.API.Read.Application.Commands.Initialform_Files;
using SmartTracking.Domain.HQX;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Read.Application.CommandHandlers.Initialform_Files
{
    /// <summary>
    /// 根据ID获取初期表单命令处理器
    /// </summary>
    public class GetInitialformByIdCommandHandler : IRequestHandler<GetInitialformByIdCommand, ApiResult<Initialform>>
    {
        private readonly IBaseRepository<Initialform> _initialformRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetInitialformByIdCommandHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="initialformRepository">初期表单仓储</param>
        /// <param name="mapper">对象映射器</param>
        /// <param name="logger">日志记录器</param>
        public GetInitialformByIdCommandHandler(
            IBaseRepository<Initialform> initialformRepository,
            IMapper mapper,
            ILogger<GetInitialformByIdCommandHandler> logger)
        {
            _initialformRepository = initialformRepository;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 处理根据ID获取初期表单命令
        /// </summary>
        /// <param name="request">根据ID获取初期表单命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>初期表单信息</returns>
        public async Task<ApiResult<Initialform>> Handle(GetInitialformByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证请求参数
                if (request == null)
                {
                    _logger.LogWarning("获取初期表单请求参数为空");
                    return new ApiResult<Initialform>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "请求参数不能为空",
                        data = default
                    };
                }

                if (request.Id <= 0)
                {
                    _logger.LogWarning($"初期表单ID无效: {request.Id}");
                    return new ApiResult<Initialform>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "初期表单ID无效",
                        data = default
                    };
                }

                // 查询初期表单
                var initialform = await _initialformRepository.GetAll().Where(i => !i.IsDeleted)
                    .FirstOrDefaultAsync(i => i.InitialId == request.Id, cancellationToken);

                if (initialform == null)
                {
                    _logger.LogWarning($"未找到初期表单，ID: {request.Id}");
                    return new ApiResult<Initialform>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "未找到指定的初期表单",
                        data = default
                    };
                }

                _logger.LogInformation($"获取初期表单成功，ID: {request.Id}");
                return new ApiResult<Initialform>
                {
                    Code = ApiEnum.查询成功,
                    Message = "获取初期表单成功",
                    data = initialform
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取初期表单时发生系统异常，ID: {request.Id}, 错误: {ex.Message}");
                return new ApiResult<Initialform>
                {
                    Code = ApiEnum.查询失败,
                    Message = "系统异常，请联系管理员",
                    data = default
                };
            }
        }
    }
}